#include <htc.h>
//#include <hitech.h>
#include <pic16f685.h>

// RB4 - start/stop button

// RC0 - output A phase +
// RC1 - output A phase -
// RC2 - output B phase +
// RC3 - output B phase -
// RC4 - output C phase +
// RC5 - output C phase -

unsigned char angle = 0;
unsigned char state = 0;
unsigned char isrun = 0;
unsigned char delay = 77;

char getstate(char angle);

void initialize(void);
void run(void);

void start(void);
void stop(void);
void clock(void);

void interrupt int_handler(void) {
	// Interrupt at TMR2
	if (TMR2IF == 1) {
		TMR2IF = 0;
		clock();
	}
	// Interrupt at PORTA or PORTB
	if (RABIF == 1) {
		// High level on RB4
		if (RB4 == 1) {
			if (isrun == 1) { 
				stop();
			} else {
				start();
			}
		}	
		// Clear flag interrupt
		RABIF = 0;
	}
}

void main (void) {
	initialize();
	run();
}

void start(void) {
	isrun = 1;
	
	//state = 0b01000000;
	// Initialize Timer2
	PEIE = 1;
	TMR2IE = 1;
	PR2 = delay;
	T2CON = 0b00000100;
}

void stop(void) {
	isrun = 0;
	TMR2ON = 0;
	PORTC = 0;
}

void clock(void) {
	//state = state >> 1;
	//if (state == 0) {state = 0b00100000;}
	//PORTC = state;
	PORTC = getstate(angle);
	angle++;
}	

void run(void) {
	while(1) {}
}

void initialize(void) {
	// Setup ports
	PORTC = 0;
	TRISC = 0b11000000;
	PORTB = 0;
	TRISB = 0xFF;
	PORTA = 0;
	TRISA = 0xFF;
	ANSEL = 0;
	ANSELH = 0;

	// Setup interrupts
	INTCON = 0;
	GIE = 1;
	RABIE = 1;
	//IOCB = 0b00010000;
	IOCB = 0xff;
	IOCA = 0xff;
}

char getstate(char angle) {
	PCL += angle;
	_asm
		retlw 8
		retlw 24
		retlw 24
		retlw 17
		retlw 24
		retlw 24
		retlw 24
		retlw 8
		retlw 8
		retlw 8
		retlw 24
		retlw 25
		retlw 25
		retlw 24
		retlw 24
		retlw 8
		retlw 8
		retlw 8
		retlw 25
		retlw 25
		retlw 25
		retlw 25
		retlw 8
		retlw 8
		retlw 8
		retlw 9
		retlw 9
		retlw 25
		retlw 25
		retlw 9
		retlw 8
		retlw 8
		retlw 8
		retlw 9
		retlw 9
		retlw 9
		retlw 17
		retlw 9
		retlw 9
		retlw 8
		retlw 9
		retlw 9
		retlw 9
		retlw 41
		retlw 1
		retlw 9
		retlw 9
		retlw 8
		retlw 9
		retlw 9
		retlw 9
		retlw 33
		retlw 33
		retlw 1
		retlw 9
		retlw 9
		retlw 9
		retlw 9
		retlw 41
		retlw 33
		retlw 33
		retlw 33
		retlw 9
		retlw 9
		retlw 9
		retlw 41
		retlw 33
		retlw 33
		retlw 33
		retlw 33
		retlw 1
		retlw 9
		retlw 9
		retlw 33
		retlw 33
		retlw 33
		retlw 33
		retlw 33
		retlw 33
		retlw 1
		retlw 40
		retlw 33
		retlw 33
		retlw 33
		retlw 33
		retlw 33
		retlw 33
		retlw 5
		retlw 32
		retlw 33
		retlw 33
		retlw 33
		retlw 33
		retlw 33
		retlw 33
		retlw 36
		retlw 36
		retlw 32
		retlw 33
		retlw 33
		retlw 33
		retlw 33
		retlw 37
		retlw 36
		retlw 36
		retlw 36
		retlw 33
		retlw 33
		retlw 33
		retlw 37
		retlw 36
		retlw 36
		retlw 36
		retlw 36
		retlw 32
		retlw 33
		retlw 33
		retlw 36
		retlw 36
		retlw 36
		retlw 4
		retlw 36
		retlw 36
		retlw 32
		retlw 37
		retlw 36
		retlw 36
		retlw 36
		retlw 4
		retlw 36
		retlw 36
		retlw 34
		retlw 36
		retlw 36
		retlw 36
		retlw 4
		retlw 4
		retlw 4
		retlw 36
		retlw 38
		retlw 38
		retlw 36
		retlw 36
		retlw 4
		retlw 4
		retlw 4
		retlw 38
		retlw 38
		retlw 38
		retlw 38
		retlw 4
		retlw 4
		retlw 4
		retlw 6
		retlw 6
		retlw 38
		retlw 38
		retlw 6
		retlw 4
		retlw 4
		retlw 4
		retlw 6
		retlw 6
		retlw 6
		retlw 34
		retlw 6
		retlw 6
		retlw 4
		retlw 6
		retlw 6
		retlw 6
		retlw 22
		retlw 2
		retlw 6
		retlw 6
		retlw 4
		retlw 6
		retlw 6
		retlw 6
		retlw 18
		retlw 18
		retlw 2
		retlw 6
		retlw 6
		retlw 6
		retlw 6
		retlw 22
		retlw 18
		retlw 18
		retlw 18
		retlw 6
		retlw 6
		retlw 6
		retlw 22
		retlw 18
		retlw 18
		retlw 18
		retlw 18
		retlw 2
		retlw 6
		retlw 6
		retlw 18
		retlw 18
		retlw 18
		retlw 18
		retlw 18
		retlw 18
		retlw 2
		retlw 20
		retlw 18
		retlw 18
		retlw 18
		retlw 18
		retlw 18
		retlw 18
		retlw 10
		retlw 16
		retlw 18
		retlw 18
		retlw 18
		retlw 18
		retlw 18
		retlw 18
		retlw 24
		retlw 24
		retlw 16
		retlw 18
		retlw 18
		retlw 18
		retlw 18
		retlw 26
		retlw 24
		retlw 24
		retlw 24
		retlw 18
		retlw 18
		retlw 18
		retlw 26
		retlw 24
		retlw 24
		retlw 24
		retlw 24
		retlw 16
		retlw 18
		retlw 18
		retlw 24
		retlw 24
		retlw 24
		retlw 8
		retlw 24
		retlw 24
		retlw 16
		retlw 26
		retlw 24
		retlw 24
		retlw 24
	_endasm
}